<?php

namespace app\controller\admin\module\match;

use app\controller\admin\module\AdminBaseController;
use think\response\Json;
use think\response\View;

/**
 * @auth-name 赛事列表
 * @auth-alias match
 */
class Index extends AdminBaseController
{
    protected string $modelName = 'match';
    /**
     * 主页
     * @auth-name 主页
     * @auth-alias index
     * @return View
     */
    public function index(): View
    {
        return fetch('/admin/match/index');
    }

    /**
     * @auth-name 列表
     * @auth-alias list
     * @return Json
     */
    public function list(): Json
    {
        $limit = values('limit', 10);
        $condition = [];
        $this->condition($condition);
        $list = $this->model->getPageList($condition, 'id, room_id, match_id, uid, opposite_uid, x, y, chess, withdraw, addtime', 'addtime desc', ['start' => ((values('page', 10) - 1) * $limit), 'limit' => $limit]);
        $userInfos = getUserList((!empty($list['rows']) ? array_filter(array_unique(array_merge(array_column($list['rows'], 'uid'), array_column($list['rows'], 'opposite_uid')))) : []));
        if(!empty($userInfos)){
            $userInfos = array_column($userInfos, null, 'id');
            foreach($list['rows'] as $k => $v){
                $list['rows'][$k]['username'] = !empty($v['uid']) ? $v['uid'] . '-' . ($userInfos[$v['uid']]['username'] ?? '-') : '-';
                $list['rows'][$k]['opposite_username'] = !empty($v['opposite_uid']) ? $v['opposite_uid'] . '-' . ($userInfos[$v['opposite_uid']]['username'] ?? '-') : '-';
            }
        }
        return json($list);
    }

    private function condition(&$condition = []){
        $uidKeywordField = values('uid_keyword_field');
        $uidKeywordValue = values('uid_keyword_value');
        (!empty($uidKeywordField) and !empty($uidKeywordValue)) and $condition[$uidKeywordField] = $uidKeywordValue;
        //房间编号
        $room_id = values('room_id');
        !empty($room_id) and $condition['room_id'] = $room_id;
        $match_id = values('match_id');
        !empty($match_id) and $condition['match_id'] = $match_id;
    }

}